# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=thrift
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=0.21.0
pkgrel=1
pkgdesc="Apache Thrift is a software framework for scalable cross-language services development (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://thrift.apache.org/'
msys2_repository_url="https://github.com/apache/thrift"
msys2_references=(
  "cpe: cpe:/a:apache:thrift"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
source=("https://archive.apache.org/dist/thrift/${pkgver}/${_realname}-${pkgver}.tar.gz"{,.asc}
        001-math-define-pi.patch
        002-fix-pkgconfig-paths.patch)
sha256sums=('9a24f3eba9a4ca493602226c16d8c228037db3b9291c6fc4019bfe3bd39fc67c'
            'SKIP'
            '1c84e10b84b3d44f5ab2e7be7505c9375f0a5516b906e2a52784d6c5c01fab08'
            '7d6f3ec1844adefaad6e96437fd921a6a6f859dd6e7f9d60259eadc90ce570cc')
validpgpkeys=('8CD87F186F06E958EFCA963D76BD340FC4B75865') # Jens Geyer <jensgeyer@hotmail.com>

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/001-math-define-pi.patch"
  patch -p1 -i "${srcdir}/002-fix-pkgconfig-paths.patch"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _extra_config+=(
    -DBUILD_COMPILER=ON
    -DBUILD_TESTING=OFF
    -DBUILD_TUTORIALS=OFF
    -DWITH_LIBEVENT=OFF
    -DWITH_OPENSSL=ON
    -DWITH_ZLIB=ON
    -DWITH_QT5=OFF
    -DWITH_CPP=ON
    -DWITH_C_GLIB=OFF
    -DWITH_JAVA=OFF
    -DWITH_JAVASCRIPT=OFF
    -DWITH_NODEJS=OFF
    -DWITH_PYTHON=OFF
  )

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}-static"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}-shared"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-shared"
}

check() {
  "${MINGW_PREFIX}"/bin/cmake -S "${_realname}-${pkgver}" -B "build-${MSYSTEM}-static" -DBUILD_TESTING=ON
  "${MINGW_PREFIX}"/bin/cmake --build "build-${MSYSTEM}-static"
  "${MINGW_PREFIX}"/bin/ctest.exe --test-dir "build-${MSYSTEM}-static" || warning "Tests failed"

  "${MINGW_PREFIX}"/bin/cmake -S "${_realname}-${pkgver}" -B "build-${MSYSTEM}-shared" -DBUILD_TESTING=ON
  "${MINGW_PREFIX}"/bin/cmake --build "build-${MSYSTEM}-shared"
  "${MINGW_PREFIX}"/bin/ctest.exe --test-dir "build-${MSYSTEM}-shared" || warning "Tests failed"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-shared"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/thrift/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
